package cn.wolfcode.crud.web.controller;

import cn.wolfcode.crud.qo.CustomerReportQuery;
import cn.wolfcode.crud.service.ICustomerReportService;
import com.alibaba.fastjson.JSON;
import org.aspectj.bridge.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;
    
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQuery qo){
        model.addAttribute("pageInfo",customerReportService.selectCustomerReport(qo));
        return "customerReport/list";
    }

    /*
    * 柱状图
    * */
    @RequestMapping("/listByBar")
    public String listByBar(Model model, @ModelAttribute("qo") CustomerReportQuery qo){
        //查询报表相关的数据
        List<HashMap> list = customerReportService.selectAll(qo);

        //提供柱状图需要的数据x员工的数据,y对应的新增客户数量
        ArrayList<String> xList = new ArrayList<>();
        ArrayList<Object> yList = new ArrayList<>();

        //遍历list封装到数据中
        for (HashMap map : list) {
            xList.add((String) map.get("groupType"));
            yList.add( map.get("number"));
        }
        model.addAttribute("xList",JSON.toJSONString(xList) );
        model.addAttribute("yList",JSON.toJSONString(yList));
        //设置分组类型
        model.addAttribute("groupTypeName", cn.wolfcode.crud.util.MessageUtil.changMsg(qo));
        return "customerReport/listByBar";
    }

    //饼状图
    @RequestMapping("/listByPie")
    public String listByPie(Model model, @ModelAttribute("qo") CustomerReportQuery qo){
        //查询报表相关的数据
        List<HashMap> list = customerReportService.selectAll(qo);
        //提供饼状图需要的数据
        ArrayList<String> legendList = new ArrayList<>();
        ArrayList<Object> serieList = new ArrayList<>();
        for (HashMap map : list) {
            legendList.add((String) map.get("groupType"));
            Map temp = new HashMap();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("number"));
            serieList.add(temp);


        }
        model.addAttribute("legendList",JSON.toJSONString(legendList) );
        model.addAttribute("serieList",JSON.toJSONString(serieList));
        //设置分组类型
        model.addAttribute("groupTypeName", cn.wolfcode.crud.util.MessageUtil.changMsg(qo));

        return "customerReport/listByPie";
    }
}	